﻿using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Joker.View.Editor {
    public partial class TypeDataEdit : Form {
        public string Method;
        public int TypeID = -1;
        public TypeDataEdit() {
            InitializeComponent();
        }

        private void TypeDataEdit_Load(object sender, EventArgs e) {
            textBox1.Text = Method;
            if (TypeID != -1) {
                DataTable tmp = DBHelper.GetDataTable($"SELECT [TypeName] WHERE [ID] = {TypeID}");
                textBox2.Text = tmp.Rows[0][0].ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            Close();
        }

        private void button2_Click(object sender, EventArgs e) {
            if (TypeID == -1) {
                string sqlstr;
                if (Method.Equals("入库")) {
                    sqlstr = $"INSERT [WarehouseInType] VALUES('{textBox2.Text}')";
                } else {
                    sqlstr = $"INSERT [WarehouseOutType] VALUES('{textBox2.Text}')";
                }
                if (DBHelper.ExecuteNonQuery(sqlstr)) {
                    MessageBox.Show("新增成功!");
                } else {
                    MessageBox.Show("新增失败!");
                }
            } else {
                string sqlstr;
                if (Method.Equals("入库")) {
                    sqlstr = $"UPDATE [WarehouseOutType] SET [TypeName] = {textBox2.Text} WHERE ID = {TypeID}";
                } else {
                    sqlstr = $"UPDATE [WarehouseInType] SET [TypeName] = {textBox2.Text} WHERE ID = {TypeID}";
                }
                if (DBHelper.ExecuteNonQuery(sqlstr)) {
                    MessageBox.Show("修改成功!");
                } else {
                    MessageBox.Show("修改失败!");
                }
            }
        }
    }
}
